<?php
namespace app\beautiful\model\order;
use app\common\model\CommonModel;
use app\beautiful\model\member\MemberModel;
use app\beautiful\model\order\GoodsOrderItemModel;
class GoodsOrderModel extends CommonModel{
    protected  $name = 'beautiful_order';
    protected  $pk   = 'order_id';
    protected $type=[
        'pay_info' => 'array',
    ];

    public function member()
    {
        return $this->hasOne(MemberModel::class,'member_id','member_id');
    }
    
    public function getPayTimeFormatAttr($value,$data){
        return $data['pay_time'] ? date('Y-m-d H:i:s',$data['pay_time']) : '-';
    }
    
    public function getOrderTimeAttr($value,$data){
        return date('Y-m-d H:i',$data['add_time']);
    }
    
    public function getDjsTimeAttr($value,$data){
        $t = time();
        $djs = $data['add_time'] + 900 - $t; //15分钟有效
        return  $djs > 0 ?  $djs : 0;
    }
    
    public function getYouhuiAttr($value,$data){
        $youhui = round(($data['integral_balance'] * 100 + $data['coupon_money'] * 100 + $data['youhui_balance'] * 100)/100,2);
        return $youhui;
    }
    
    public function skus(){
        return  $this->hasMany(GoodsOrderItemModel::class,'order_id','order_id');
    }
    
    public function getPayTypeMeanAttr($value,$data)
    {
        $type_means=[
            'weixin'=>'微信支付',
            'money'=>'余额支付',
            'offline' => '代客下单',
        ];
        return $type_means[$data['pay_type']] ?? '-';
    }

    public function getStatusMeansAttr( $value,$data)
    {
        $status_means=[
            1=>'待付款',
            2=>'待发货',
            3=>'待收货',
            4=>'待自提',
            8=>'已完成',
            -1=>'已取消',
        ];
        return $status_means[$data['status']] ?? '-';
    }

    public function getOrderTypeMeansAttr($value,$data)
    {
        $order_type_means=[
            'normal'=>'商品订单',
            'pre_sale'=>'预售订单',
            'flash_sale'=>'秒杀订单',
            'pin_tuan'=>'拼团订单',
        ];
        return $order_type_means[$data['order_type']] ?? '-';
    }
}

